package com.bit.librarymanagementsystem.Controller;


import com.bit.librarymanagementsystem.Constant.Constant;
import com.bit.librarymanagementsystem.DAO.BookDAO;
import com.bit.librarymanagementsystem.Entity.BookInfo;
import com.bit.librarymanagementsystem.Service.BookService;
import com.bit.librarymanagementsystem.model.PageRequest;
import com.bit.librarymanagementsystem.model.PageResult;
import com.bit.librarymanagementsystem.model.Result;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    BookService bookService;

    @RequestMapping("/getlist")
    public List<BookInfo> getList(){
        BookService bookService=new BookService();
        List<BookInfo> bookInfoList=bookService.getList();
        return bookInfoList;
    }

    @RequestMapping("/addBook")
    public String addBook(BookInfo bookInfo){
        //参数校验
        if(bookInfo==null
                || !StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount()==null
                || bookInfo.getCount()==null
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus()==null
        ){
            return "输入参数错误,请检查输入!";
        }
        log.info("添加图书:{}",bookInfo);//输出普通类必须添加{}
        //插入图书
        try{
            bookService.addBook(bookInfo);
            return "";
        }catch (Exception e){
            log.error("添加图书失败:",e);//异常类可以不用{}
            return e.getMessage();
        }

    }

    @RequestMapping("/getListByPage")
    public Result<PageResult<BookInfo>>getListByPage(PageRequest pageRequest, HttpSession session){
        log.info("获取图书列表,pageRequest:{}",pageRequest);
//        //判断用户是否登录
//        if(!StringUtils.hasLength((String)session.getAttribute(Constant.SESSION_LOGIN_USER_NAME))){
//            return Result.unLogin();
//        }

        PageResult<BookInfo> pageResult=bookService.getListByPage(pageRequest);
        return Result.success(pageResult);
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
            log.info("查询图书信息,bookId:{}",bookId);
            if (bookId==null||bookId<1){
                return null;
            }
            return bookService.queryBookById(bookId);
    }

    @RequestMapping("/updateBook")
    public String updateBook(BookInfo bookInfo){
        log.info("更新图书信息,bookInfo:{}",bookInfo);
        Integer result= bookService.updateBook(bookInfo);
        try {
            if(result<1){
                return "图书更新失败,请联系管理员";
            }else {
                return "";
            }
        }catch (Exception e){
            log.error("图书更新发生异常,e:",e);
            return "图书更新失败,请联系管理员";
        }
    }

    @RequestMapping("/deleteBook")
    public String deleteBook(Integer bookId){
        log.info("删除图书信息,bookInfo:{}",bookId);
        Integer result= bookService.deleteBook(bookId);
        try {
            if(result<1){
                return "图书删除失败,请联系管理员";
            }else {
                return "";
            }
        }catch (Exception e){
            log.error("图书删除发生异常,e:",e);
            return "图书删除失败,请联系管理员";
        }
    }

    @RequestMapping("batchDeleteBook")
    public String batchDeleteBook(@RequestParam List<Integer> ids){
        Integer result=bookService.batchDeleteBook(ids);
       return result>0?"":"批量删除错误,请联系管理员";
    }

}
